package com.neusoft.yiyang.bed.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.yiyang.bed.client.CustomerClient;
import com.neusoft.yiyang.bed.mapper.BedUsageMapper;
import com.neusoft.yiyang.common.entity.bed.Bed;
import com.neusoft.yiyang.bed.mapper.BedMapper;
import com.neusoft.yiyang.bed.service.BedService;
import com.neusoft.yiyang.common.entity.bed.BedUsage;
import com.neusoft.yiyang.common.entity.bed.vto.BedBedUsage;
import com.neusoft.yiyang.common.entity.bed.vto.BedCustomer;
import com.neusoft.yiyang.common.entity.bed.vto.BedCustomerUsage;
import com.neusoft.yiyang.common.entity.customer.Customer;
import com.neusoft.yiyang.common.enums.BedStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BedServiceImpl implements BedService {
    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private BedUsageMapper bedUsageMapper;

    @Autowired
    private CustomerClient customerClient;

    @Override
    public void createBed(Bed bed) {
        bedMapper.insert(bed);
    }

    @Override
    public void updateBed(Bed bed) {
        bedMapper.updateById(bed);
    }

    @Override
    public void deleteBed(Integer bedId) {
        Bed bed = new Bed();
        bed.setBedId(bedId);
        bed.setIsDeleted(1);  // 逻辑删除
        bedMapper.updateById(bed);
    }

    @Override
    public Bed getBedById(Integer bedId) {
        return bedMapper.selectById(bedId);
    }

    @Override
    public List<Bed> getAllBeds() {
        return bedMapper.selectList(null);
    }

    @Override
    // 获取指定楼栋和楼层的所有不同房间号
    public List<String> getDistinctRoomsByFloor(Integer buildingNo, Integer floor) {
        return bedMapper.getDistinctRoomsByFloor(buildingNo, floor);
    }
    @Override
    // 根据楼栋、楼层和房间号获取状态为free的床位
    public List<Bed> getAvailableBedsByRoom(Integer buildingNo, Integer floor, String roomNumber) {
        return bedMapper.getAvailableBedsByRoom(buildingNo, floor, roomNumber);
    }
    @Override
    public Bed getBedByParams(Integer buildingNo, Integer floor, String roomNumber, Integer bedNo) {
        Bed bed = bedMapper.selectByParams(buildingNo, floor, roomNumber, bedNo);
        return bed;
    }

    @Override
    public List<Bed> searchBeds(Integer buildingNo, Integer floor, String roomNumber) {
        // 创建查询条件构造器
        QueryWrapper<Bed> queryWrapper = new QueryWrapper<>();

        // 动态添加查询条件
        if (buildingNo != null) {
            queryWrapper.eq("building_no", buildingNo);
        }

        if (floor != null) {
            queryWrapper.eq("floor", floor);
        }

        if (roomNumber != null) {
            queryWrapper.eq("room_number", roomNumber);
        }

        // 只查询未被删除的床位
        queryWrapper.eq("is_deleted", 0);

        // 执行查询
        return bedMapper.selectList(queryWrapper);
    }

    // 宫获得指定楼层的床位和顾客信息
    @Override
    public List<BedCustomer> getBedCustomerByFloor(Integer floor) {
        List<Bed> beds = bedMapper.selectByFloorId(floor);
        List<Customer> customers = customerClient.getCustomerByBed(beds);
        Map<Integer, Customer> map = new HashMap<>();
        List<BedCustomer> bedCustomers = new ArrayList<>();
        for (Customer customer : customers) {
            map.put(customer.getBedId(), customer);
        }
        for (Bed bed : beds) {
            if (map.containsKey(bed.getBedId())) {
                Customer customer = map.get(bed.getBedId());
                bedCustomers.add(new BedCustomer(bed, customer));
            } else {
                bedCustomers.add(new BedCustomer(bed, new Customer()));
            }
        }
        return bedCustomers;
    }

    // 宫获得out occupied床位和顾客信息
    @Override
    public List<BedCustomerUsage> getAllBedCustomerUsageOutOccupied() {
        List<BedBedUsage> bedBedUsages = bedMapper.getBedBedUsageLatest();
        List<Customer> customers = customerClient.getCustomerHasBed();
        List<BedCustomerUsage> bedCustomerUsages = new ArrayList<>();
        Map<Integer, Customer> map = new HashMap<>();
        for (Customer customer : customers) {
            map.put(customer.getCustomerId(), customer);
        }
        for (BedBedUsage bedBedUsage : bedBedUsages) {
            if (map.containsKey(bedBedUsage.getCustomerId())) {
                Customer customer = map.get(bedBedUsage.getCustomerId());
                bedCustomerUsages.add(new BedCustomerUsage(customer, bedBedUsage));
            }
        }
        return bedCustomerUsages;
    }

    @Override
    public List<Integer> getFreeFloor() {
        return bedMapper.getFreeFloor();
    }

    @Override
    public List<String> getFreeRoom(Integer floorNumber) {
        return bedMapper.getFreeRoom(floorNumber);
    }

    @Override
    public List<Integer> getFreeBed(String roomNumber) {
        return bedMapper.getFreeBed(roomNumber);
    }

    @Override
    public List<Bed> getByBedIds(List<Integer> bedIds) {
        if (bedIds == null || bedIds.isEmpty()) {
            return Collections.emptyList();
        }
        QueryWrapper<Bed> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("bed_id", bedIds);
        return bedMapper.selectList(queryWrapper);
    }

    @Override
    public List<Bed> getAllFreeBeds() {

        QueryWrapper<Bed> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", BedStatus.free);
        queryWrapper.eq("is_deleted", 0);
        return bedMapper.selectList(queryWrapper);
    }

    @Override
    public List<BedCustomerUsage> getAllBedCutomerUsage() {
        List<Customer> customers = customerClient.searchCustomers(null, null, null, null);
        List<BedUsage> bedUsages = bedUsageMapper.selectList(null);
        List<Bed> beds = bedMapper.selectList(null);
        Map<Integer, Customer> map = new HashMap<>();
        Map<Integer, Bed> map2 = new HashMap<>();
        for (Customer customer : customers) {
            map.put(customer.getCustomerId(), customer);
        }
        for (Bed bed : beds) {
            map2.put(bed.getBedId(), bed);
        }
        List<BedCustomerUsage> bedCustomerUsages = new ArrayList<>();
        for (BedUsage bedUsage : bedUsages) {
            if (map.containsKey(bedUsage.getCustomerId())) {
                Customer customer = map.get(bedUsage.getCustomerId());
                Bed bed = map2.get(bedUsage.getBedId());
                if (customer.getIsDeleted() != 1) {
                    bedCustomerUsages.add(new BedCustomerUsage(customer, new BedBedUsage(bed, bedUsage)));
                }
            }
        }
        return bedCustomerUsages;
    }


    @Override
    public Integer getBedIdByLocation(Integer buildingNo, Integer floor, String roomNumber,Integer bedNo){
        QueryWrapper<Bed> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("building_no", buildingNo)
                .eq("floor", floor)
                .eq("room_number", roomNumber)
                .eq("bed_no", bedNo)
                .select("bed_id"); // 只查询id字段

        Bed bed = bedMapper.selectOne(queryWrapper);
        return bed != null ? bed.getBedId() : null;
    }
}
